﻿var btnAddBoard = document.getElementById("btnAddBoard");
var btnRemoveBoard = document.getElementById("btnRemoveBoard");
var lstSelectedBoards = document.getElementById("lstSelectedBoards");
CheckButtonEnable();

function btnAddBoard_Click() {
    var ChosenBoardID = null;
    for (var i = 0; i < lstBoards.length; i++) {
        if (lstBoards.options[i].selected) {
            ChosenBoardID = lstBoards.options[i].value;
            lstSelectedBoards.options[lstSelectedBoards.length] = new Option(lstBoards.options[i].text, ChosenBoardID);
            break;
        }
    }
    SetBoardSelection(ChosenBoardID, true);
    CheckButtonEnable();
}

function btnRemoveBoard_Click() {
    for (var i = 0; i < lstSelectedBoards.length; i++) {
        var BoardOption = lstSelectedBoards.options[i];
        if (BoardOption.selected) {
            var RemoveBoardID = BoardOption.value;
            SetBoardSelection(RemoveBoardID, false);
            lstSelectedBoards.options[i] = null;
            CheckButtonEnable();
            break;
        }
    }
}

function BoardsChange() {
    CheckButtonEnable();
}

function SelectedBoardsChanged() {
    CheckButtonEnable();
}

function SetBoardSelection(BoardID, IsSelected) {
    if (BoardID != null) {
        for (var i = 0; i < Secretariats.length; i++) {
            var Boards = Secretariats[i].Boards;
            for (var j = 0; j < Boards.length; j++) {
                var Board = Boards[j];
                if (Board.ID == BoardID)
                    Board.IsSelected = IsSelected;
            }
        }
        ShowBoards()
    }
}

function SelectAll(lstSelectedBoards) {
    for (var i = 0; i < lstSelectedBoards.length; i++) {
        lstSelectedBoards.options[i].selected = true;
    }
}

function CheckButtonEnable() {
    var NoSelections = true;
    if (lstSelectedBoards.length < 3) {
        for (var i = 0; i < lstBoards.length; i++) {
            if (i > 0 && lstBoards.options[i].selected) {
                NoSelections = false;
                break;
            }
        }
        btnAddBoard.disabled = NoSelections;
    } else {
        btnAddBoard.disabled = true;
    }

    NoSelections = true;
    for (var i = 0; i < lstSelectedBoards.length; i++) {
        if (lstSelectedBoards.options[i].selected) {
            NoSelections = false;
            break;
        }
    }
    btnRemoveBoard.disabled = NoSelections;
}

// Client-side validation for Boards custom validator
function BoardSelected(sender, args) {
    if (document.getElementById("lstSelectedBoards").length == 0) {
        args.IsValid = false;
        return;
    }

    args.IsValid = true;
}